﻿Imports System.IO
Imports DevExpress.XtraEditors

Public Class frmOperationServer_Form
    Public id_host As Long

    Private Sub frmOperationServer_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Cursor = Cursors.WaitCursor

        If id_host <> -1 Then
            If File.Exists(My.Application.Info.DirectoryPath.ToString & "\client.xml") = False Then client_write_configuration()
            client_read_configuration()
            If mysql_isconnected() Then
                Dim data As DataTable = mysql_query(String.Format("SELECT * FROM tb_host WHERE id_host = '{0}'", id_host))
                TextEditHost.Text = data.Rows(0)("host").ToString
                TextEditPort.Text = data.Rows(0)("port").ToString
                TextEditUsername.Text = data.Rows(0)("username").ToString
                TextEditPassword.Text = data.Rows(0)("password").ToString
                SimpleButtonSave.Enabled = True

                TextEditName.Text = data.Rows(0)("nama_host").ToString

            End If
        Else
            TextEditHost.Text = ""
            TextEditUsername.Text = ""
            TextEditPassword.Text = ""
            TextEditName.Text = ""

            SimpleButtonSave.Enabled = True
        End If

        Cursor = Cursors.Default
    End Sub

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        Cursor = Cursors.WaitCursor

        Dim host As String = TextEditHost.Text
        Dim port As String = TextEditPort.Text
        Dim username As String = TextEditUsername.Text
        Dim password As String = TextEditPassword.Text
        Dim name As String = TextEditName.Text

        If Not isStrEmpty(New String() {host, port, username, password, name}) Then
            XtraMessageBox.Show("Please fill all required field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            If mysql_isconnected(False, host, Convert.ToInt32(port), username, password) Then
                If id_host = -1 Then
                    mysql_non_query(String.Format("INSERT INTO tb_host(nama_host,host,port,username,password) VALUES('{0}','{1}','{2}','{3}','{4}')", name, host, port, username, password))
                Else
                    mysql_non_query(String.Format("UPDATE tb_host SET nama_host = '{0}', host = '{1}', port = '{2}', username = '{3}', password = '{4}' WHERE id_host = '{5}'", name, host, port, username, password, id_host))
                End If

                Me.Close()
            Else
                XtraMessageBox.Show("Can't Connect to Database Server!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            
            frmOperationServer.view_list()
        End If

        Cursor = Cursors.Default
    End Sub
End Class